package com.aitou.mrylive.presentation.ui.login;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding.view.RxView;
import com.aitou.mrylive.R;
import com.aitou.mrylive.presentation.ui.base.BaseActivity;
import com.aitou.mrylive.presentation.ui.main.MainActivity;
import com.aitou.mrylive.util.Const;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rx.functions.Action1;
import rx.functions.Func1;

//手机登录页面
public class LoginActivity extends BaseActivity implements LoginUiInterface {
    private EditText mName, mPass, mCaptcha;
//                                        获取验证码的按钮
    private Button mLogin, mRegister, mGetCaptcha;
    private CountDownTimer countDownTimer;

    private LoginPresenter presenter;

    public static Intent createIntent(Context context) {
        return new Intent(context, LoginActivity.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void findViews(Bundle savedInstanceState) {
        mName = (EditText) findViewById(R.id.login_name);
        mPass = (EditText) findViewById(R.id.login_pass);
        mCaptcha = (EditText) findViewById(R.id.login_captcha);
        mGetCaptcha = (Button) findViewById(R.id.login_captcha_countdown);
        mLogin = (Button) findViewById(R.id.login_btn);
        mRegister = (Button) findViewById(R.id.login_register_btn);


        mName.setInputType(InputType.TYPE_CLASS_NUMBER);
//        listenInput();

    }

    @Override
    protected void onResume() {
//        MobclickAgent.onPageStart("手机登录");
        super.onResume();
    }

    @Override
    protected void onPause() {
//        MobclickAgent.onPageEnd("手机登录");
        super.onPause();
    }

    @Override
    protected void setListeners() {
        super.setListeners();
//        listenInput();
//        mName.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                listenInput();
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        mPass.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                listenInput();
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
        RxView.clicks(mGetCaptcha)
                .throttleFirst(Const.VIEW_THROTTLE_TIME, TimeUnit.MILLISECONDS)
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        if (TextUtils.isEmpty(mName.getText())){
                            toastShort("你还没有输入手机号");
                            return Boolean.FALSE;
                        }else if (!isMobileNO(mName.getText().toString())){
                            toastShort("请输入正确的手机号");
                            return Boolean.FALSE;
                        }
                        return countDownTimer == null;
                    }
                })
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String phone = mName.getText().toString().trim();
                        presenter.sendCaptcha(phone);
                        countDownTimer = new CountDownTimer(60000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                mGetCaptcha.setText(getString(R.string.login_captcha_countdown,
                                        millisUntilFinished / 1000));
                            }

                            @Override
                            public void onFinish() {
                                mGetCaptcha.setText(R.string.login_captcha_get);
                                countDownTimer = null;
                            }
                        }.start();
                    }
                });


        RxView.clicks(mLogin).throttleFirst(Const.VIEW_THROTTLE_TIME,
                TimeUnit.MILLISECONDS)
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        if (TextUtils.isEmpty(mName.getText())) {
                            toastShort("用户名不能为空");
                            return Boolean.FALSE;
                        }
                        if (TextUtils.isEmpty(mCaptcha.getText())) {
                            toastShort("你还没填写验证码");
                            return Boolean.FALSE;
                        }
                        return Boolean.TRUE;
                    }
                })
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String name = mName.getText().toString().trim();
//                        String pwd = mPass.getText().toString().trim();
                        String captcha = mCaptcha.getText().toString().trim();
                        presenter.loginByCaptcha(name, captcha);
                    }
                });
        RxView.clicks(mRegister).throttleFirst(Const.VIEW_THROTTLE_TIME,
                TimeUnit.MILLISECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Intent i = RegisterActivity.createIntent(LoginActivity.this);
                startActivity(i);
            }
        });
    }

    @Override
    protected void init() {
        setSwipeBackEnable(false);
        presenter = new LoginPresenter(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unsubscribeTasks();
    }

    //    private void listenInput() {
//        if (TextUtils.isEmpty(mName.getText()) || TextUtils.isEmpty(mPass.getText())) {
//            mLogin.setEnabled(false);
//        } else {
//            mLogin.setEnabled(true);
//        }
//    }

    @Override
    public void startActivityAndFinishOthers() {
        startActivity(MainActivity.createIntent(LoginActivity.this));
        sendFinishBroadcast(MainActivity.class.getSimpleName());
    }

    public boolean isMobileNO(String mobiles){
        Pattern p = Pattern.compile("^1[3|4|5|7|8]\\d{9}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
}
